package com.cy.config;


import com.cy.filter.MyRolesFilter;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.filter.authc.LogoutFilter;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.crazycake.shiro.RedisCacheManager;
import org.crazycake.shiro.RedisManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * shiro配置类
 * @author wxhn1
 */
@Configuration
public class ShiroConfig {

    /**
     * 安全管理器配置类 核心配置
     * @return
     */
    @Bean
    public DefaultWebSecurityManager dwsm(){
        DefaultWebSecurityManager dwsm = new DefaultWebSecurityManager();
        // 会话管理器  如果不设置 第一次请求服务器会报错
        dwsm.setSessionManager(new MySessionManager());
        // 设置域
        dwsm.setRealm(mr());
        return dwsm;
    }

    @Bean
    public MysqlRealm mr(){
        MysqlRealm mr = new MysqlRealm();
        // 设置登录时的加密方式
        HashedCredentialsMatcher hcm = new HashedCredentialsMatcher();
        hcm.setHashAlgorithmName("sha-256");// 加密方式
        hcm.setHashIterations(10);// 散列次数
        mr.setCredentialsMatcher(hcm);
        // 设置权限缓存到redis
//        RedisCacheManager rcm = new RedisCacheManager();
//        rcm.setRedisManager(new RedisManager());
//        mr.setCacheManager(rcm);
        return mr;
    }

    /**
     * shiro过滤器工厂对象 必须配置 且组件名必须叫 shiroFilterFactoryBean
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(){
        ShiroFilterFactoryBean sff = new ShiroFilterFactoryBean();
        // 设置安全管理器
        sff.setSecurityManager(dwsm());
        // 需要登录 未登录的跳转地址 页面地址或控制器地址
        sff.setLoginUrl("");
        // 没有授权的地址
        sff.setUnauthorizedUrl("");
        return sff;
    }
}
